package JFrame.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

import JFrame.Tools;
import serive.UserService;
import serive.UserServiceImpl;

public class ChangeUserInformation extends JFrame {
    private JLabel l_name, l_password1, l_password2, l_sex, l_address,l_age, l_idCard,l_telephoneNumber;
    private JTextField t_name,t_address,t_age,t_idCard,t_telephoneNumber;
    private JPasswordField p_password1, p_password2;
    ButtonGroup bg;
    private JRadioButton r_male, r_female;
    private JButton b_reg, b_reset;
    UserService userService =new UserServiceImpl();
    JFrame jFrame= this;
    String curUserId;

    public ChangeUserInformation(String curUserId) {
        this.curUserId=curUserId;
        this.setTitle("快递派送系统_用户修改页面");
        this.setSize(500, 800);
        this.setLocation(100, 150);
        init();
        this.setLocationRelativeTo(null);  // 如果组件当前未显示或者 c 为 null，则窗体居中显示
        this.setVisible(true);
    }
    public void init() {
        this.setLayout(new GridLayout(9, 4, 5, 5));

        l_name = new JLabel("姓名", JLabel.CENTER);
        l_idCard = new JLabel("居民身份证", JLabel.CENTER);
        l_password1 = new JLabel("密码", JLabel.CENTER);
        l_password2 = new JLabel("确认密码", JLabel.CENTER);
        l_sex = new JLabel("性别", JLabel.CENTER);
        l_age = new JLabel("年龄", JLabel.CENTER);
        l_telephoneNumber = new JLabel("电话", JLabel.CENTER);
        l_address = new JLabel("地址", JLabel.CENTER);


        t_name = new JTextField();
        t_address =new JTextField();
        t_age=new JTextField();
        t_idCard=new JTextField();
        t_telephoneNumber=new JTextField();

        p_password1 = new JPasswordField();
        p_password2 = new JPasswordField();

        JPanel p = new JPanel();
        bg = new ButtonGroup();
        r_male = new JRadioButton("男");
        r_female = new JRadioButton("女");
        bg.add(r_male);
        bg.add(r_female);

        b_reg = new JButton("修改");
        b_reg.setBackground(new Color(102,204,255));
        b_reg.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String sex=null; // 提前定义，下面要用
                Enumeration<AbstractButton> bgElements = bg.getElements();
                while (bgElements.hasMoreElements()) {
                    AbstractButton btn = bgElements.nextElement();
                    // 返回切换按钮的状态，如果按钮被选中则true，否则false
                    if(btn.isSelected()){
                        sex=btn.getText();
                        break;
                    }
                }
                if (sex == null){
                    JOptionPane.showMessageDialog(jFrame.getContentPane(),  // 获得JFrame的内容面板
                            "请选择性别", "！！！",JOptionPane.WARNING_MESSAGE); // 感叹号图标
                }

                // (Integer.parseInt) String类型的整数转化成int类型的整数
                boolean isModify = userService.modify(curUserId, p_password1.getText(), p_password2.
                                getText(), t_idCard.getText(), sex, Integer.parseInt(t_age.getText()), t_address.getText(), t_name.getText(),
                        t_telephoneNumber.getText());
                if (isModify){
                    JOptionPane.showMessageDialog(jFrame.getContentPane(),
                            "修改成功", "！！！",JOptionPane.WARNING_MESSAGE);
                    jFrame.dispose(); // setVisible方法仅仅隐藏窗体，而dispose方法是关闭窗体，并释放一部分资源。
                }else{
                    JOptionPane.showMessageDialog(jFrame.getContentPane(),
                            "错误。请输入你的正确的信息", "！！！",JOptionPane.WARNING_MESSAGE);
                }

            }

        });
        b_reset = new JButton("重置");
        b_reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Tools.emptying( t_name,t_address,t_age,t_idCard,t_telephoneNumber,p_password1, p_password2);
            }
        });
        b_reset.setBackground(new Color(102,204,255));

        p.add(r_male);
        p.add(r_female);

        Tools.setFonts(18,1, l_name, l_password1, l_password2, l_sex, l_address,l_age,
                l_idCard,l_telephoneNumber,r_male, r_female, b_reg, b_reset);
        Tools.addComponents(this,l_name,t_name,l_password1,p_password1,l_password2,p_password2,
                l_address,t_address,l_sex,p, l_age,t_age, l_idCard,t_idCard,l_telephoneNumber,t_telephoneNumber,
                b_reg,b_reset);
    }
}
